package com.myl.admin.controller.sys.auth;

import com.myl.admin.service.ISysRoleService;
import com.myl.core.basic.BaseController;
import com.myl.core.domain.entity.SysRole;
import com.myl.core.domain.model.TablePageList;
import com.myl.core.utils.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台角色表 控制器
 * </p>
 *
 * @author Myl <ixiaomu@qq.com>
 * @date 2024-01-10 11:21:21
 */
@Tag(name = "角色管理")
@Slf4j
@RestController
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    @Autowired
    private ISysRoleService iSysRoleService;

    /**
     * 列表
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "列表")
    @GetMapping("list")
    public ResponseResult<?> list(
            @Parameter(description = "页码") @RequestParam(name="current", defaultValue = "1") Integer current,
            @Parameter(description = "每页显示数量") @RequestParam(name="pageSize", defaultValue = "10") Integer pageSize,
            @Parameter(description = "关键词") @RequestParam(name="keyword", defaultValue = "") String keyword
    ) {
        TablePageList data = iSysRoleService.getListWithPage(current, pageSize, keyword);
        return ResponseResult.success(data);
    }

    /**
     * 详情
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
    */
    @Operation(summary = "获取详情")
    @GetMapping("info")
    public ResponseResult<?> getInfo(@RequestParam Long id) {
        SysRole info = iSysRoleService.getById(id);
        return ResponseResult.success(info);
    }

    /**
     * 新增
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "新增 SysRole")
    @PostMapping(value = "add")
    public ResponseResult<?> add(
            @Validated @RequestBody() SysRole entity
    ) {
        SysRole info = iSysRoleService.saveOrUpdateById(entity);
        return ResponseResult.success(info);
    }

    /**
     * 编辑
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "编辑")
    @PostMapping("edit")
    public ResponseResult<?> edit(
            @Validated @RequestBody() SysRole entity
    ) {
        Assert.notNull(entity.getId(), "ID 不能为空");
        if (iSysRoleService.checkFieldUnique(entity, "name", entity.getName())){
            return ResponseResult.fail("名称已存在");
        }
        if (iSysRoleService.checkFieldUnique(entity, "alias", entity.getAlias())){
            return ResponseResult.fail("名标识已存在");
        }
        SysRole info = iSysRoleService.saveOrUpdateById(entity);
        return ResponseResult.success(info);
    }

    /**
     * （批量）删除 后台角色表
     *
     * @author Myl <ixiaomu@qq.com>
     * @date 2024-01-10 11:21:21
     */
    @Operation(summary = "（批量）删除")
    @DeleteMapping("delete")
    public ResponseResult<?> delete(
            @RequestBody() List<Integer> ids
    ) {
        iSysRoleService.deleteBatchByIds(ids);
        return ResponseResult.success("操作成功");
    }

}
